﻿namespace EasyCMS.Core.Dals.Subject
{
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Collections.Generic;
    using Pismire.Components;
    using Pismire;
    using Pismire.Data;

    public class EasyCMS_SqlServer2005 : Sql2005DalBase, ISubject
    {
        #region ISubject 成员

        public void Init(string args)
        {
            this.PartitionMark = args;
        }

        /// <summary>
        /// 主题内容所在分区标识
        /// </summary>
        public string PartitionMark
        {
            get
            {
                return m_PartitionMark;
            }
            set
            {
                m_PartitionMark = value;
                DataTableName = "[Subjects_" + value + "]";
            }
        }
        private string m_PartitionMark;

        /// <summary>
        /// 主题内容所在分区
        /// </summary>
        public string DataTableName { get; private set; }

        public EasyCMS.Common.DataComponents.Subject GetByID(int id)
        {
            //初始化
            EasyCMS.Common.DataComponents.Subject data = null;

            //打开并取得连接，并执行查询操作
            dcm.Open();

            //获取数据
            IDataReader reader = null;
            SqlCommand cmd = null;
            try
            {
                //创建cmd
                cmd = GetSqlCmd();
                cmd.CommandText = "select top 1 * from " + DataTableName + " Where ID = @ID";
                cmd.Parameters.Add("@ID", SqlDbType.Int).Value = id;

                //执行cmd，取得reader
                reader = cmd.ExecuteReader();

                //绑定数据
                if (reader != null && reader.Read())
                {
                    DataPopulater<EasyCMS.Common.DataComponents.Subject> builder = DataPopulater<EasyCMS.Common.DataComponents.Subject>.CreatePopulater(reader);

                    data = builder.Populate(reader);
                }
            }
            catch (Exception ex)
            {
                //异常记日志
                EasyCMS.Common.Runtime.WriteLog(Pismire.Loger.LogType.Error, "读取数据库产生异常", ex);

                //debug模式则抛出异常
                #if DEBUG
                throw ex;
                #endif
            }
            finally
            {
                //关闭与释放reader
                if (reader != null)
                {
                    if (!reader.IsClosed) reader.Close();
                    reader.Dispose();
                    reader = null;
                }

                //释放cmd
                if (cmd != null)
                {
                    cmd.Dispose();
                    cmd = null;
                }

                //通知连接管理器关闭数据库连接
                dcm.Close();
            }

            //返回数据
            return data;
        }

        public EasyCMS.Common.DataComponents.Subject GetByCodeName(string codeName)
        {
            //初始化
            EasyCMS.Common.DataComponents.Subject data = null;

            //打开并取得连接，并执行查询操作
            dcm.Open();

            //获取数据
            IDataReader reader = null;
            SqlCommand cmd = null;
            try
            {
                //创建cmd
                cmd = GetSqlCmd();
                cmd.CommandText = "select top 1 * from " + DataTableName + " Where CodeName = @CodeName";
                cmd.Parameters.Add("@CodeName", SqlDbType.NVarChar).Value = codeName;

                //执行cmd，取得reader
                reader = cmd.ExecuteReader();

                //绑定数据
                if (reader != null && reader.Read())
                {
                    DataPopulater<EasyCMS.Common.DataComponents.Subject> builder = DataPopulater<EasyCMS.Common.DataComponents.Subject>.CreatePopulater(reader);

                    data = builder.Populate(reader);
                }
            }
            catch (Exception ex)
            {
                //异常记日志
                EasyCMS.Common.Runtime.WriteLog(Pismire.Loger.LogType.Error, "读取数据库产生异常", ex);

                //debug模式则抛出异常
                #if DEBUG
                throw ex;
                #endif
            }
            finally
            {
                //关闭与释放reader
                if (reader != null)
                {
                    if (!reader.IsClosed) reader.Close();
                    reader.Dispose();
                    reader = null;
                }

                //释放cmd
                if (cmd != null)
                {
                    cmd.Dispose();
                    cmd = null;
                }

                //通知连接管理器关闭数据库连接
                dcm.Close();
            }

            //返回数据
            return data;
        }

        public List<EasyCMS.Common.DataComponents.Subject> GetList(EasyCMS.Common.Querys.Subject query, out int recordCount)
        {
            //初始化定义
            recordCount = 0;
            List<EasyCMS.Common.DataComponents.Subject> dataList = new List<EasyCMS.Common.DataComponents.Subject>();

            //拼装查询sql
            #warning 拼装sql没完成

            //打开连接连接
            dcm.Open();

            //获取数据
            IDataReader reader = null;
            SqlCommand cmd = null;
            try
            {
                //创建cmd
                cmd = GetSqlCmd();
                cmd.CommandText = "EasyCMS_Subject_GetList";
                cmd.CommandType = CommandType.StoredProcedure;

                //设置参数
                cmd.Parameters.Add("@DataTableName", SqlDbType.NVarChar).Value = this.DataTableName;

                //执行cmd，取得reader
                reader = cmd.ExecuteReader();

                //绑定数据
                if (reader != null)
                {
                    DataPopulater<EasyCMS.Common.DataComponents.Subject> builder = DataPopulater<EasyCMS.Common.DataComponents.Subject>.CreatePopulater(reader);

                    while (reader.Read())
                    {
                        dataList.Add(builder.Populate(reader));
                    }
                }
            }
            catch (Exception ex)
            {
                //异常记日志
                EasyCMS.Common.Runtime.WriteLog(Pismire.Loger.LogType.Error, "读取数据库产生异常", ex);

                //debug模式则抛出异常
                #if DEBUG
                throw ex;
                #endif
            }
            finally
            {
                //关闭与释放reader
                if (reader != null)
                {
                    if (!reader.IsClosed) reader.Close();
                    reader.Dispose();
                    reader = null;
                }

                //释放cmd
                if (cmd != null)
                {
                    cmd.Dispose();
                    cmd = null;
                }

                //通知连接管理器关闭数据库连接
                dcm.Close();
            }

            //返回数据
            return dataList;
        }

        public ExcuteState Add(ref EasyCMS.Common.DataComponents.Subject data)
        {
            //初始化
            ExcuteState state = new ExcuteState();

            //打开数据库连接
            dcm.Open();

            //执行数据库操作
            SqlCommand cmd = null;
            try
            {
                //创建cmd
                cmd = GetSqlCmd();
                cmd.CommandText = "EasyCMS_Subject_Add";
                cmd.CommandType = CommandType.StoredProcedure;

                //设置参数
                SqlParameter paramID = cmd.Parameters.Add("@ID", SqlDbType.Int);
                paramID.Direction = ParameterDirection.Output;
                cmd.Parameters.Add("@DataTableName", SqlDbType.NVarChar).Value = this.DataTableName;
                cmd.Parameters.Add("@CodeName", SqlDbType.NVarChar).Value = data.CodeName;

                //执行
                string resultCode = cmd.ExecuteScalar().ToString();

                //判断resultCode
                switch (resultCode)
                {
                    case "succeed":
                        break;
                    case "failed":
                        state.Succeed = false;
                        state.Message = "未知错误";
                        return state;
                }

                //获取出参
                data.ID = (int)paramID.Value;
            }
            catch (Exception ex)
            {
                //异常记日志
                Pismire.Loger.Write("", ex.ToString(), Pismire.Loger.LogType.Warn);

                //返回错误
                state.Succeed = false;
                state.Message = "操作发生了异常，提示信息：" + ex.Message;
                state.InnerException = ex;
                return state;
            }
            finally
            {
                //释放cmd
                if (cmd != null)
                {
                    cmd.Dispose();
                    cmd = null;
                }

                //通知连接管理器关闭数据库连接
                dcm.Close();
            }

            //返回
            return state;
        }

        public ExcuteState Update(ref EasyCMS.Common.DataComponents.Subject data)
        {
            //初始化
            ExcuteState state = new ExcuteState();

            //打开数据库连接
            dcm.Open();

            //执行数据库操作
            SqlCommand cmd = null;
            try
            {
                //创建cmd
                cmd = GetSqlCmd();
                cmd.CommandText = "EasyCMS_Subject_Update";
                cmd.CommandType = CommandType.StoredProcedure;

                //设置参数
                cmd.Parameters.Add("@ID", SqlDbType.Int).Value = data.ID;
                cmd.Parameters.Add("@DataTableName", SqlDbType.NVarChar).Value = this.DataTableName;
                cmd.Parameters.Add("@CodeName", SqlDbType.NVarChar).Value = data.CodeName;

                //执行
                string resultCode = cmd.ExecuteScalar().ToString();

                //判断resultCode
                switch (resultCode)
                {
                    case "succeed":
                        break;
                    case "failed":
                        state.Succeed = false;
                        state.Message = "未知错误";
                        return state;
                }
            }
            catch (Exception ex)
            {
                //异常记日志
                Pismire.Loger.Write("", ex.ToString(), Pismire.Loger.LogType.Warn);

                //返回错误
                state.Succeed = false;
                state.Message = "操作发生了异常，提示信息：" + ex.Message;
                state.InnerException = ex;
                return state;
            }
            finally
            {
                //释放cmd
                if (cmd != null)
                {
                    cmd.Dispose();
                    cmd = null;
                }

                //通知连接管理器关闭数据库连接
                dcm.Close();
            }

            //返回
            return state;
        }

        public ExcuteState Delete(int id)
        {
            //初始化
            ExcuteState state = new ExcuteState();

            //打开数据库连接
            dcm.Open();

            //执行数据库操作
            SqlCommand cmd = null;
            try
            {
                //创建cmd
                cmd = GetSqlCmd();
                cmd.CommandText = "EasyCMS_Subject_Delete";
                cmd.CommandType = CommandType.StoredProcedure;

                //设置参数
                cmd.Parameters.Add("@ID", SqlDbType.Int).Value = id;
                cmd.Parameters.Add("@DataTableName", SqlDbType.NVarChar).Value = this.DataTableName;

                //执行
                string resultCode = cmd.ExecuteScalar().ToString();

                //判断resultCode
                switch (resultCode)
                {
                    case "succeed":
                        break;
                    case "failed":
                        state.Succeed = false;
                        state.Message = "未知错误";
                        return state;
                }
            }
            catch (Exception ex)
            {
                //异常记日志
                Pismire.Loger.Write("", ex.ToString(), Pismire.Loger.LogType.Warn);

                //返回错误
                state.Succeed = false;
                state.Message = "操作发生了异常，提示信息：" + ex.Message;
                state.InnerException = ex;
                return state;
            }
            finally
            {
                //释放cmd
                if (cmd != null)
                {
                    cmd.Dispose();
                    cmd = null;
                }

                //通知连接管理器关闭数据库连接
                dcm.Close();
            }

            //返回
            return state;
        }

        public ExcuteState CreateSubjectTable()
        {
            //初始化
            ExcuteState state = new ExcuteState();

            //打开数据库连接
            dcm.Open();

            //
            SqlCommand cmd = null;
            try
            {
                //创建cmd
                cmd = GetSqlCmd();
                cmd.CommandText = "EasyCMS_Subject_CreateSubjectTable";
                cmd.CommandType = CommandType.StoredProcedure;

                //设置参数
                cmd.Parameters.Add("@DataTableName", SqlDbType.NVarChar).Value = this.DataTableName;

                //执行
                string resultCode = cmd.ExecuteScalar().ToString();

                //判断resultCode
                switch (resultCode)
                {
                    case "succeed":
                        break;
                    case "failed":
                        state.Succeed = false;
                        state.Message = "未知错误";
                        return state;
                }
            }
            catch (Exception ex)
            {
                //异常记日志
                Pismire.Loger.Write("",ex.ToString(), Pismire.Loger.LogType.Warn);

                //返回错误
                state.Succeed = false;
                state.Message = "操作发生了异常，提示信息：" + ex.Message;
                state.InnerException = ex;
                return state;
            }
            finally
            {
                //释放cmd
                if (cmd != null)
                {
                    cmd.Dispose();
                    cmd = null;
                }

                //通知连接管理器关闭数据库连接
                dcm.Close();
            }

            //返回
            return state;
        }

        #endregion
    }
}
